package com.bwie.controller;

import cn.hutool.core.io.resource.StringResource;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.utils.R;
import com.bwie.utils.Result;
import com.bwie.vo.LoginVo;
import com.bwie.vo.UserPageVo;
import com.google.zxing.PlanarYUVLuminanceSource;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: TaoShiHao
 * @Date: 2025/5/21 15:39
 * @Description:
 **/
@RestController
@RequestMapping("/auth/index")
@Api(tags = "认证服务 ")
public class IndexController {

    @Autowired
    UserService userService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result login(@RequestBody @Validated LoginVo loginVo, BindingResult result) {
        //校验
        if (result.hasErrors()) {
            return Result.error().setMsg(result.getFieldError().getDefaultMessage());
        }
        //验证用户存不存在
        User user = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getUserName, loginVo.getUserName()));
        if (user == null) {
            return Result.error().setMsg("用户不存在");
        }
        //加密
        String md5Hex = DigestUtil.md5Hex(loginVo.getUserPass());
        System.out.println("加密后的密码:" + md5Hex);
        if (!md5Hex.equals(user.getUserPass())) {
            return Result.error().setMsg("密码错误");
        }
        //生成token
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", user.getUserId())
                .claim("userName", user.getUserName())
                .claim("nickName", user.getNickName())
                .compact();

        String tokenKey = "token-" + user.getUserId();
        stringRedisTemplate.opsForValue().set(tokenKey, token, 60, TimeUnit.MINUTES);
        user.setUserPass(null);
        user.setToken(token);
        return Result.ok().setData(user);

    }

    @PostMapping("/getMenuTree")
    @ApiOperation(value = "获取登录人的权限列表")
    public Result getMenuTree(HttpServletRequest request) {
        return userService.getMenuTree(request);
    }

    @PostMapping("/getUserList")
    @ApiOperation(value = "分页获取商户列表")
    public Result getUserList(@RequestBody UserPageVo userPageVo) {
        return userService.getUserList(userPageVo);
    }

    @PostMapping("/amendUser")
    @ApiOperation(value = "修改商户")
    public Result amendUser(@RequestBody User user) {
        return userService.amendUser(user);
    }
    @PostMapping("/uploadFile")
    @ApiOperation(value = "图片上传")
    public Result uploadFile(@RequestPart(name = "file") MultipartFile file) {
        return userService.upload(file);
    }

    @PostMapping("/deleteUser/{userId}")
    @ApiOperation(value = "删除用户")
    public Result deleteUser(@PathVariable(name = "userId") Integer userId){
      return   userService.deleteUser(userId);
    }
    @PostMapping("/deleteUserAll")
    @ApiOperation(value = "批量删除用户")
    public Result deleteUserAll(@RequestBody List<User> userList){
        return userService.deleteUserAll(userList);
    }

    @PostMapping("/checkUser")
    @ApiOperation(value = "商户审核")
    public Result checkUser(@RequestBody User user){
        return userService.checkUser(user);
    }




}
